/**
 * Copyright(C) 2012 Luvina Software Company
 *
 * TblUserLogicImpl.java, 28/12/2012, bacdv
 */
package net.luvina.manageuser.logic.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import net.luvina.manageuser.dao.TblDetailUserJapanDao;
import net.luvina.manageuser.dao.TblUserDao;
import net.luvina.manageuser.dto.UserDetailDto;
import net.luvina.manageuser.hibernate.TblDetailUserJapan;
import net.luvina.manageuser.hibernate.TblUser;
import net.luvina.manageuser.logic.TblDetailUserJapanLogic;
import net.luvina.manageuser.logic.TblUserLogic;

/**
 * get list user
 *
 * @author bacdv
 *
 */
@Service("tblUserLogic")
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, readOnly = false, rollbackFor = { Throwable.class })
public class TblUserLogicImpl implements TblUserLogic {
    @Autowired
    private TblUserDao tblUserDao;
    @Autowired
    private TblDetailUserJapanLogic tblDetailUserJapanLogic;
    @Autowired
    private TblDetailUserJapanDao tblDetailUserJapanDao;

    /*
     * (non-Javadoc)
     *
     * @see net.luvina.manageuser.logic.TblUserLogic#getListUser(int, int, int,
     * java.lang.String, java.lang.String, java.lang.String, java.lang.String,
     * java.lang.String)
     */
    public List<UserDetailDto> getListUser(final int offset, final int limit,
            final int groupId, final String fullName, final String sortType,
            final String sortByFullName, final String sortByCodeLevel,
            final String sortByEndDate) {
        return tblUserDao.getListUser(offset, limit, groupId, fullName,
                sortType, sortByFullName, sortByCodeLevel, sortByEndDate);
    }

    /*
     * (non-Javadoc)
     *
     * @see net.luvina.manageuser.logic.TblUserLogic#getTotalRecords(int,
     * java.lang.String)
     */
    public int getTotalRecords(int groupId, String fullName) {
        return tblUserDao.getTotalRecords(groupId, fullName);
    }

    /*
     * (non-Javadoc)
     *
     * @see
     * net.luvina.manageuser.logic.TblUserLogic#createUser(net.luvina.manageuser
     * .hibernate.TblUser)
     */
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, readOnly = false, rollbackFor = Throwable.class)
    public boolean createUser(TblUser tblUser) {
        if (tblUser == null) {
            return false;
        }
        boolean result = true;
        int userId = this.tblUserDao.create(tblUser);
        TblDetailUserJapan tbldetailUserJapan = tblUser.getTblDetailUserJapan();
        if (tbldetailUserJapan != null
                && !"0".equals(tbldetailUserJapan.getCodeLevel())) {
            tbldetailUserJapan.setUserId(userId);
            try {
                this.tblDetailUserJapanLogic
                        .createDetailUserJapan(tbldetailUserJapan);
            } catch (Exception e) {
                result = false;
            }
        }
        return result;
    }

    /*
     * (non-Javadoc)
     *
     * @see
     * net.luvina.manageuser.logic.TblUserLogic#updateUser(net.luvina.manageuser
     * .hibernate.TblUser)
     */
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, readOnly = false, rollbackFor = Throwable.class)
    public boolean updateUser(TblUser tblUser) {
        if (tblUser == null) {
            return false;
        }
        boolean result = true;
        try {
            this.tblUserDao.update(tblUser);
        } catch (Exception e) {
            result = false;
        }
        TblDetailUserJapan tblDetailUserJapan = tblUser.getTblDetailUserJapan();
        try {
            int detailId = tblDetailUserJapan.getDetailUserJapanId();
            if (detailId > 0) {
                this.tblDetailUserJapanDao.delete(tblDetailUserJapan);
            }
            if (!"0".equals(tblDetailUserJapan.getCodeLevel())) {
                tblDetailUserJapan.setUserId(tblUser.getUserId());
                this.tblDetailUserJapanLogic
                        .createDetailUserJapan(tblDetailUserJapan);
            }
        } catch (Exception e) {
            result = false;
        }
        return result;
    }

    /*
     * (non-Javadoc)
     *
     * @see
     * net.luvina.manageuser.logic.TblUserLogic#deleteUser(net.luvina.manageuser
     * .hibernate.TblUser)
     */
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, readOnly = false, rollbackFor = Throwable.class)
    public boolean deleteUser(TblUser tblUser) {
        boolean result = true;
        int userId = tblUser.getUserId();
        if (userId > 0) {
            TblDetailUserJapan tblDetailUserJapan = this.tblDetailUserJapanDao
                    .findTblDetailJapanById(tblUser);
            if (tblDetailUserJapan != null) {
                try {
                    tblDetailUserJapanDao.delete(tblDetailUserJapan);
                } catch (Exception e) {
                    result = false;
                }
            }
            try {
                tblUserDao.delete(tblUser);
            } catch (Exception e) {
                result = false;
            }
        }
        return result;
    }

    /*
     * (non-Javadoc)
     *
     * @see net.luvina.manageuser.logic.TblUserLogic#getUserById(int)
     */
    public TblUser getUserById(int userId) {
        TblUser tblUser = tblUserDao.read(userId);
        return tblUser;
    }

    /*
     * (non-Javadoc)
     *
     * @see net.luvina.manageuser.logic.TblUserLogic#getDetailUserById(int)
     */
    public UserDetailDto getDetailUserById(int userId) {
        UserDetailDto userDetailDto = tblUserDao.getDetailUser(userId);
        return userDetailDto;
    }

    /*
     * (non-Javadoc)
     *
     * @see net.luvina.manageuser.logic.TblUserLogic#checkExistedEmail(int,
     * java.lang.String)
     */
    public boolean checkExistedEmail(int userId, String email) {
        TblUser tblUser = tblUserDao.getUserByEmail(userId, email);
        if (tblUser != null) {
            return true;
        } else {
            return false;
        }
    }

    /*
     * (non-Javadoc)
     *
     * @see net.luvina.manageuser.logic.TblUserLogic#checkExistedLoginName(int,
     * java.lang.String)
     */
    public boolean checkExistedLoginName(int userId, String loginName) {
        TblUser tblUser = tblUserDao.checkExistedLoginName(userId, loginName);
        if (tblUser != null) {
            return true;
        } else {
            return false;
        }
    }

}
